if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.keyMgt.viewEdit.keystoreEdit")) {
    tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/keyMgt/viewEdit/jss/jss.xml", "tibco.admin", "jss29B", "ljss", false);
    jsx3.Class.defineClass(
            'tibco.admin.enterpriseAssets.keyMgt.viewEdit.keystoreEdit', // full name of the class to create
            jsx3.gui.Block, //  extends jsx3.lang.Object
            null, // dose not implements any interfaces
            function(keystoreEdit) {

                keystoreEdit.prototype.entityID = null;
                keystoreEdit.prototype.reloadInProgress = false;
                keystoreEdit.prototype.addInProgress = false;
                keystoreEdit.prototype.editInProgress = false;

                keystoreEdit.prototype.subscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.keystore.EventID.KEYSTORE_SELECTED, me, me.loadParams);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.keystore.EventID.KEYSTORE_ADD, me, me.goIntoAddMode);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.keystore.EventID.KEYSTORE_REFRESH, me, me.viewMode);
                    this.clearDetails();
                }

                keystoreEdit.prototype.populateFormElement = function(objName, value) {
                    var object = this.getDescendantOfName(objName);
                    if (object instanceof jsx3.gui.TextBox) {
                        object.setValue(value);
                        object.getParent().getChild(1).setText(value, true);
                    } else {
                        object.setText(value, true);
                        var textboxes = object.getParent().getDescendantsOfType(jsx3.gui.TextBox, true);
                        if (textboxes && textboxes.length > 0) {
                            textboxes[0].setValue(value);
                        }
                    }
                    return object;
                }

                keystoreEdit.prototype.editMode = function(textfield, toShow) {
                    textfield.setDisplay(toShow?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true).
                            getParent().getChild(1).setDisplay(toShow?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK, true);
                    return textfield;
                }

                keystoreEdit.prototype.setTypeDisplay = function(internalOrExternal, editMode) {
                    this.getDescendantOfName("lblKeystoreType_29B").setText(internalOrExternal, true);
                    var block = this.getDescendantOfName("blkExternal29B");
                    if (internalOrExternal == tibco.admin.Main.getServer().getDynamicProperty("@lblkeystoreExternal29A")) {
                        block.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).getParent().
                                getChild("blkInternal29B").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.editMode(block.getDescendantOfName("txtKeystoreURL_29B"), editMode);
                    } else {
                        block.setDisplay(jsx3.gui.Block.DISPLAYNONE, true).getParent().
                                getChild("blkInternal29B").setDisplay(editMode?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true);
                    }
                    jsx3.require("tibco.admin.enterpriseAssets.keyMgt.viewEdit.KeystoreUpload");
                    tibco.admin.enterpriseAssets.keyMgt.viewEdit.KeystoreUpload.initIFrame();

                }

                keystoreEdit.prototype.viewMode = function(objEvent) {
                    if (this.editInProgress) {
                        this.editKeystoreDetails(false);
                    }
                    if (this.reloadInProgress) {
                        this.resetReload();
                    }
                    if (this.addInProgress) {
                        this.resetAdd();
                    }
                    this.clearDetails();
                }

                keystoreEdit.prototype.loadParams = function(objEvent) {
                    this.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    if (this.editInProgress) {
                        this.editKeystoreDetails(false);
                    }
                    if (this.reloadInProgress) {
                        this.resetReload();
                    }
                    if (objEvent.entityID) {
                        //common elements first
                        this.getDescendantOfName("btnEditKeystore_29B").setDisplay((objEvent.hasEdit == 'true')?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true).
                                setEnabled((objEvent.hasOwner=='true')?jsx3.gui.Form.STATEENABLED:jsx3.gui.Form.STATEDISABLED, true);;
                        this.entityID = objEvent.entityID;
                        this.populateFormElement("txtKeystoreName_29B", objEvent.keystoreName);
                        this.populateFormElement("txtKeystoreDescription_29B", objEvent.keystoreDescription);
                        this.getDescendantOfName("lblKeystorePassword_29B").setText("<i>" + tibco.admin.Main.getServer().getDynamicProperty("@@lbl_Hidden_29B") + "</i>", true);
                        this.setTypeDisplay(objEvent.keystoreType, false);
                        if (objEvent.keystoreType == tibco.admin.Main.getServer().getDynamicProperty("@lblkeystoreExternal29A")) {
                            this.getDescendantOfName("btnRefreshKeyStore_29B").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                            this.populateFormElement("txtKeystoreURL_29B", objEvent.keystoreURL);
                        } else {
                            var button = this.getDescendantOfName("btnRefreshKeyStore_29B").setEnabled((objEvent.hasOwner == 'true')?jsx3.gui.Form.STATEENABLED:jsx3.gui.Form.STATEDISABLED, true);
                            button.setDisplay((objEvent.hasOwner == 'true')?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true);
                        }

                        this.getDescendantOfName("blkMessage29B").setText("", true);
                    } else {     //when no rows are selected
                        this.clearDetails();
                    }
                }

                keystoreEdit.prototype.clearDetails = function() {
                    this.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.getDescendantOfName("lblKeystoreType_29B").setText("", true);
                    this.getDescendantOfName("blkMessage29B").setText("", true);
                    this.populateFormElement("txtKeystoreName_29B", "");
                    this.populateFormElement("txtKeystoreDescription_29B", "");
                    this.populateFormElement("txtKeystorePassword_29B", "");
                    this.populateFormElement("txtKeystoreLocation_29B", "");
                    this.populateFormElement("txtKeystoreURL_29B", "");
                    this.getDescendantOfName("btnEditKeystore_29B").setEnabled(jsx3.gui.Form.STATEDISABLED, true).setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.getDescendantOfName("btnRefreshKeyStore_29B").setEnabled(jsx3.gui.Form.STATEDISABLED, true).setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.setTypeDisplay("", false);
                }

                keystoreEdit.prototype.reloadKeystore = function() {
                    this.handler.endEdit();
                    jsx3.require("tibco.admin.enterpriseAssets.keyMgt.viewEdit.KeystoreUpload");
                    tibco.admin.enterpriseAssets.keyMgt.viewEdit.KeystoreUpload.doSubmit();
                    msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.keystore.EventID.DISABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);

                }

                keystoreEdit.prototype.calleditKeystoreRegisteredWithEnterprise = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/keyMgt/viewEdit/rules/editKeystoreRules.xml",
                            "editKeystoreRegisteredWithEnterprise",
                            "Edit Keystores Registered With Enterprise");
                    //subscribe and call
                    objService.entityID = this.entityID;
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.oneditKeystoreRegisteredWithEnterpriseSuccess);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getKeystoreEndpointURL());
                    objService.doCall();
                };

                keystoreEdit.prototype.oneditKeystoreRegisteredWithEnterpriseSuccess = function(objEvent) {
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.keystore.EventID.ADD_OVER;
                    tibco.admin.PubSub.publish(msg);
                    this.getDescendantOfName("btnEditKeystore_29B").focus();
                    this.resetKeystoreDesc(true);
                }

                keystoreEdit.prototype.goIntoAddMode = function(objEvent) {
                    this.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    var addMode = (!objEvent.addMode);
                    if (addMode) {
                        this.namePassed = false;
                        this.passwordPassed = false;
                    }
                    this.addInProgress = true;
                    this.getDescendantOfName("btnSaveKeystore_29B").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    this.getDescendantOfName("paneEdit29B").
                            setDisplay(addMode?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK, true).
                            getParent().getChild("paneAdd29B").
                            setDisplay(addMode?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true).
                            getParent().getChild("paneReload29B").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.editMode(this.populateFormElement("txtKeystoreName_29B", ""), addMode);
                    this.editMode(this.populateFormElement("txtKeystoreDescription_29B", ""), addMode);
                    this.editMode(this.populateFormElement("txtKeystoreURL_29B", ""), addMode);
                    this.setTypeDisplay(objEvent.keystoreType, addMode);
                    this.populateFormElement("txtKeystoreURL_29B", "");
                    this.editMode(this.populateFormElement("txtKeystorePassword_29B", ""), addMode);
                }

                keystoreEdit.prototype.resetAdd = function() {
                    this.addInProgress = false;
                    var event = new Object();
                    event.addMode = "addMode";
                    //dummy value
                    this.goIntoAddMode(event);
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.keystore.EventID.ADD_OVER;
                    tibco.admin.PubSub.publish(msg);
                    msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.keystore.EventID.ENABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);
                    this.getDescendantOfName("blkMessage29B").setText("", true);
                }

                keystoreEdit.prototype.resetReload = function() {
                    this.goIntoReloadMode(false);
                }

                keystoreEdit.prototype.editKeystoreDetails = function(editMode) {
                    this.editInProgress = editMode;
                    if (editMode)
                        this.handler.beginEdit();
                    else
                        this.handler.endEdit();
                    var editButton = this.getDescendantOfName("btnEditKeystore_29B");
                    editButton.getParent().getChild("btnSaveEditedKeystore_29B").
                            setDisplay(editMode?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true);
                    editButton.getParent().getChild("btnCancelEditedKeystore_29B").
                            setDisplay(editMode?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true)

                    editButton.setDisplay(editMode?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK, true);
                    var descText = this.getDescendantOfName("txtKeystoreDescription_29B");
                    this.editMode(descText, editMode);
                    if (this.getDescendantOfName("lblKeystoreType_29B").getText() == tibco.admin.Main.getServer().getDynamicProperty("@lblkeystoreExternal29A")) {
                        this.editMode(this.getDescendantOfName("txtKeystoreURL_29B"), editMode);
                        this.editMode(this.getDescendantOfName("txtKeystorePassword_29B"), editMode);
                    } else {
                        editButton.getParent().getChild("btnRefreshKeyStore_29B").
                                setDisplay(editMode?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK, true);
                    }
                    msg = new Object();
                    msg.subject = editMode?tibco.admin.enterpriseAssets.keystore.EventID.DISABLED_REFRESH:tibco.admin.enterpriseAssets.keystore.EventID.ENABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);
                    return descText;
                }

                keystoreEdit.prototype.resetKeystoreDesc = function(toSave) {
                    var desctext = this.editKeystoreDetails(false);
                    var desclbl = desctext.getParent().getChild(1);
                    if (toSave) {
                        desclbl.setText(desctext.getValue(), true);
                    }
                    else {
                        desctext.setValue(desclbl.getText())
                    }
                }
                keystoreEdit.prototype.goIntoReloadMode = function(flag) {
                    this.reloadInProgress = flag;
                    if (flag) {
                        this.handler.beginEdit();
                    } else {
                        this.handler.endEdit();
                    }
                    var editPane = this.getDescendantOfName("paneEdit29B");
                    editPane.setDisplay(flag?jsx3.gui.Block.DISPLAYNONE:jsx3.gui.Block.DISPLAYBLOCK, true).
                            getParent().getChild("paneReload29B").setDisplay(flag?jsx3.gui.Block.DISPLAYBLOCK:jsx3.gui.Block.DISPLAYNONE, true);
                    this.setTypeDisplay(this.getDescendantOfName("lblKeystoreType_29B").getText(), flag);
                    this.editMode(this.getDescendantOfName("txtKeystorePassword_29B"), flag);
                    msg = new Object();
                    msg.subject = flag?tibco.admin.enterpriseAssets.keystore.EventID.DISABLED_REFRESH:tibco.admin.enterpriseAssets.keystore.EventID.ENABLED_REFRESH;
                    tibco.admin.PubSub.publish(msg);
                }

                /*070818
                * if user doesn't fishish all the required fields, the "save" button will be disabled.
                */
                keystoreEdit.prototype.passwordPassed = false;
                keystoreEdit.prototype.namePassed = false;

                keystoreEdit.prototype.valiPassword = function(obj, strValue) {
                    this.passwordPassed = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@lbl_KeystorePassword_29B"));
                    this.enableSaveBtn4Add();
                }

                keystoreEdit.prototype.valiName = function(obj, strValue) {
                    this.namePassed = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@lbl_KeystoreName_29B"));
                    this.enableSaveBtn4Add();
                }

                keystoreEdit.prototype.enableSaveBtn4Add = function() {
                    if (this.passwordPassed && this.namePassed) this.getDescendantOfName("btnSaveKeystore_29B").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    else this.getDescendantOfName("btnSaveKeystore_29B").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                }

            })
}
